package easy;

public class Solution_1455 {

    public int isPrefixOfWord(String sentence, String searchWord) {
        sentence += " ";
        char[] array = searchWord.toCharArray();
        char[] chars = sentence.toCharArray();
        boolean next = false;
        for (int i = 0, j = 0, count = 0; i < chars.length; i++) {
            if (chars[i] == ' ') {
                next = false;
                j = 0;
                count++;
            } else {
                if (!next) {
                    if (chars[i] != array[j]) {
                        next = true;
                    } else {
                        j++;
                    }
                }
                if (j == array.length) {
                    return count + 1;
                }
            }
        }
        return -1;
    }

    /*public int isPrefixOfWord(String sentence, String searchWord) {
        String[] strings = sentence.split(" ");
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].startsWith(searchWord)) {
                return i + 1;
            }
        }
        return -1;
    }*/

    public static void main(String[] args) {
        Solution_1455 model = new Solution_1455();
        System.out.println(model.isPrefixOfWord("i love eating burger", "burg"));
        System.out.println(model.isPrefixOfWord("this problem is an easy problem", "pro"));
        System.out.println(model.isPrefixOfWord("i am tired", "you"));
        System.out.println(model.isPrefixOfWord("i use triple pillow", "pill"));
        System.out.println(model.isPrefixOfWord("hello from the other side", "they"));
    }
}
